﻿using BW.WMS.Model;
using BW.WMS.RBAC;
using BW.WMS.XKY;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Modeling;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.Identity;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.OpenIddict.EntityFrameworkCore;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.TenantManagement;
using Volo.Abp.TenantManagement.EntityFrameworkCore;

namespace BW.WMS.EntityFrameworkCore;

[ReplaceDbContext(typeof(IIdentityDbContext))]
[ReplaceDbContext(typeof(ITenantManagementDbContext))]
[ConnectionStringName("Default")]
public class WMSDbContext :
    AbpDbContext<WMSDbContext>,
    IIdentityDbContext,
    ITenantManagementDbContext
{
    /* Add DbSet properties for your Aggregate Roots / Entities here. */

    #region Entities from the modules

    /* Notice: We only implemented IIdentityDbContext and ITenantManagementDbContext
     * and replaced them for this DbContext. This allows you to perform JOIN
     * queries for the entities of these modules over the repositories easily. You
     * typically don't need that for other modules. But, if you need, you can
     * implement the DbContext interface of the needed module and use ReplaceDbContext
     * attribute just like IIdentityDbContext and ITenantManagementDbContext.
     *
     * More info: Replacing a DbContext of a module ensures that the related module
     * uses this DbContext on runtime. Otherwise, it will use its own DbContext class.
     */

    //Identity
    public DbSet<IdentityUser> Users { get; set; }
    public DbSet<IdentityRole> Roles { get; set; }
    public DbSet<IdentityClaimType> ClaimTypes { get; set; }
    public DbSet<OrganizationUnit> OrganizationUnits { get; set; }
    public DbSet<IdentitySecurityLog> SecurityLogs { get; set; }
    public DbSet<IdentityLinkUser> LinkUsers { get; set; }
    public DbSet<IdentityUserDelegation> UserDelegations { get; set; }
    public DbSet<IdentitySession> Sessions { get; set; }
    // Tenant Management
    public DbSet<Tenant> Tenants { get; set; }
    public DbSet<TenantConnectionString> TenantConnectionStrings { get; set; }
    public DbSet<UserModel> UserModels { get; set; }
    public DbSet<RoleModel> RoleModels { get; set; }
#region Model
    public DbSet<Dm_Client> Dm_Clients { get; set;}
    public DbSet<Dm_Code> Dm_Codes { get; set;}
    public DbSet<Dm_Dep> Dm_Deps { get; set;}
    public DbSet<Dm_Encoderule> Dm_Encoderules { get; set;}
    public DbSet<Dm_Ent> Dm_Ents { get; set;}
    public DbSet<Dm_Item> Dm_Items { get; set;}
    public DbSet<Dm_ItemType> Dm_ItemTypes { get; set;}
    public DbSet<Dm_ItemUnit> Dm_ItemUnits { get; set;}
    public DbSet<Dm_Job> Dm_Jobs { get; set;}
    public DbSet<Dm_Librarylocation> Dm_Librarylocations { get; set;}
    public DbSet<Dm_Unit> Dm_Units { get; set;}
    public DbSet<DM_User> Dm_Users { get; set;}
    public DbSet<Dm_Warehouser> dm_Warehousers { get; set;}
    public DbSet<EncoderuleInfo> EncoderuleInfos { get; set;}
    public DbSet<Me_PurchaseHd> Me_PurchaseHds { get; set;}
    public DbSet<Me_PurchaseSub> Me_PurchaseSubs { get; set;}
    public DbSet<Me_SaleHd> Me_SaleHds { get; set;}
    public DbSet<Me_SaleSub> Me_SaleSubs { get; set;}
    public DbSet<Me_StCur> Me_StCurs { get; set;}
    public DbSet<Me_StHd> Me_StHds { get; set;}
    public DbSet<Me_StSub> Me_StSubs { get; set;}
    #endregion
    #endregion

    public WMSDbContext(DbContextOptions<WMSDbContext> options)
        : base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        /* Include modules to your migration db context */

        builder.ConfigurePermissionManagement();
        builder.ConfigureSettingManagement();
        builder.ConfigureBackgroundJobs();
        builder.ConfigureAuditLogging();
        builder.ConfigureIdentity();
        builder.ConfigureOpenIddict();
        builder.ConfigureFeatureManagement();
        builder.ConfigureTenantManagement();

        /* Configure your own tables/entities inside here */

        //builder.Entity<YourEntity>(b =>
        //{
        //    b.ToTable(WMSConsts.DbTablePrefix + "YourEntities", WMSConsts.DbSchema);
        //    b.ConfigureByConvention(); //auto configure for the base class props
        //    //...
        //});
        builder.Entity<UserModel>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "UserModel", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            b.Property(x=>x.UserName).IsRequired().HasMaxLength(50);
            //auto configure for the base class props
            //...
        });

        builder.Entity<RoleModel>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "RoleModel", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            //auto configure for the base class props
            //...
        });


        #region Model
        builder.Entity<Dm_Client>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Dm_Client", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            //auto configure for the base class props
            //...
        });

        builder.Entity<Dm_Code>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Dm_Code", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            //auto configure for the base class props
            //...
        });

        builder.Entity<Dm_Dep>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Dm_Dep", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            //auto configure for the base class props
            //...
        });

        builder.Entity<Dm_Encoderule>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Dm_Encoderule", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            //auto configure for the base class props
            //...
        });

        builder.Entity<Dm_Ent>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Dm_Ent", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            //auto configure for the base class props
            //...
        });

        builder.Entity<Dm_Item>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Dm_Item", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            //auto configure for the base class props
            //...
        });
        builder.Entity<Dm_ItemType>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Dm_ItemType", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            //auto configure for the base class props
            //...
        });
        builder.Entity<Dm_ItemUnit>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Dm_ItemUnit", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            //auto configure for the base class props
            //...
        });

        builder.Entity<Dm_Job>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Dm_Job", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            //auto configure for the base class props
            //...
        });

        builder.Entity<Dm_Librarylocation>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Dm_Librarylocation", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            //auto configure for the base class props
            //...
        });

        builder.Entity<Dm_Unit>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Dm_Unit", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            //auto configure for the base class props
            //...
        });

        builder.Entity<DM_User>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "DM_User", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            //auto configure for the base class props
            //...
        });

        builder.Entity<Dm_Warehouser>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Dm_Warehouser", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            //auto configure for the base class props
            //...
        });

        builder.Entity<EncoderuleInfo>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "EncoderuleInfo", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            //auto configure for the base class props
            //...
        });

        builder.Entity<Me_PurchaseHd>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Me_PurchaseHd", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            //auto configure for the base class props
            //...
        });


        builder.Entity<Me_PurchaseSub>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Me_PurchaseSub", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            //auto configure for the base class props
            //...
        });

        builder.Entity<Me_SaleHd>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Me_SaleHd", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            //auto configure for the base class props
            //...
        });

        builder.Entity<Me_SaleSub>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Me_SaleSub", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            //auto configure for the base class props
            //...
        });

        builder.Entity<Me_StCur>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Me_StCur", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            //auto configure for the base class props
            //...
        });

        builder.Entity<Me_StHd>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Me_StHd", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            //auto configure for the base class props
            //...
        });

        builder.Entity<Me_StSub>(b =>
        {
            b.ToTable(WMSConsts.DbTablePrefix + "Me_StSub", WMSConsts.DbSchema);
            b.ConfigureByConvention();
            //auto configure for the base class props
            //...
        });

        #endregion
    }
}
